﻿/******************** NG.Respate(C)  **********************************
* 产品名称: Respate 科技
* 文件名称: main.c
* 程序作者: 雷帕特公司(南宫子萍)
* 程序版本: V1.1														   
* 编制日期: 2019/10/10
* 功能描述: 线性表链式结构
* QQ:		2085827949
**********************************************************************/
#include "LinkList.h"



//创建链表
LinkList* LinkList_Create()
{
	LinkList* list = NULL;
	list = (LinkList*)calloc(sizeof(LinkList),1);
	if (list == NULL) return NULL;
	list->data = calloc(sizeof(int),1);
	list->next = NULL;
	return list;
}

//删除链表
void LinkList_Destroy(LinkList* list)
{
	if (list == NULL) return;
	LinkList_Clear(list);//删除链表缓冲区
	free(list);
}

//清空链表
void LinkList_Clear(LinkList* list)
{
	if (list == NULL) return;
	while (LinkList_Length(list))
		LinkList_Delete(list, 0);//删除链表缓冲区

	memset(list, 0, sizeof(LinkList));
	list->data = 0;
	list->next = NULL;
}


//链表长度
int LinkList_Length(LinkList* list)
{
	return *((int*)list->data);
}



int LinkList_Insert(LinkList* list,const LinkListNode* node, int index)
{
	int i = 0;
	LinkList* current = NULL;
	LinkList* newNode= NULL;

	if (list == NULL || node == NULL || index<0) return -1;
	if (index > *((int*)list->data)) index = *((int*)list->data);	//容错

	current = (LinkList*)list;//注意

	for (i = 0; i < index&&current->next; i++)
		current = (LinkList*)current->next;

	newNode = (LinkList*)calloc(sizeof(LinkList),1);
	newNode->next = (LinkList*)current->next;
	current->next = (LinkList*)newNode;

	newNode->data = node;	//挂接数据
	
	*(int*)list->data+=1;	//长度加一
	return index;
}


//得到节点
LinkListNode* LinkList_Get(LinkList* list, int index)
{
	int i = 0;
	LinkList* current = NULL;

	if (list == NULL || index < 0) return NULL;
	if (index >= *((int*)list->data)) return NULL;

	current = (LinkListNode*)list->next;
	for (i = 0; i < index && current->next; i++)
		current = (LinkListNode*)current->next;
	
	return (LinkListNode*)current->data;
}


//删除节点
LinkListNode* LinkList_Delete(LinkList* list, int index)
{
	LinkList* current = NULL;
	LinkList* node = NULL;
	LinkListNode* data = NULL;
	if (list == NULL || index < 0) return NULL;
	if (index >= *((int*)list->data)) return NULL;

	current = (LinkListNode*)list;
	while (current->next&&(index--)>0)
		current = (LinkListNode*)current->next;

	node = (LinkListNode*)current->next;
	current->next = (LinkListNode*)node->next;
	*((int*)list->data)-=1;
	data = (LinkListNode*)node->data;
	//释放缓冲区
	free(node);
	return data;
}


//删除节点
LinkListNode* LinkList_DeleteByNode(LinkList* list, LinkListNode* data)
{
	LinkList* current = NULL;
	LinkList* node = NULL;

	if (list == NULL || data == NULL) return NULL;

	current = (LinkListNode*)list;
	while (current->next)
	{
		node = current->next;
		if (node->data == data) break;
		current = (LinkListNode*)current->next;
	}
	
	if(current->data==NULL) return NULL;

	node = (LinkListNode*)current->next;
	current->next = (LinkListNode*)node->next;
	
	*((int*)list->data)-=1;
	free(node);	//释放缓冲区
	return data;
}




